from practice.Python_函数练习.中国农业银行账户管理系统.中国农业银行账户管理系统 import *

from 辅助函数 import *


"""
开户
"""
def open_account(name,address): #输入信息除了：账号，密码，账户类型，开户行，余额
    value = insert_user(name, address)
    if value != 3:
        return value
    print("库满")

"""
开户测试
print(open_account())
print(bank[1])
"""


"""
存钱
"""
def add_balance(user_name,money):
    idx = serchUserIdxByUser_name(user_name)
    if  idx == 0:
        return False
    balance1 = bank[idx]["balance"] #存钱以前的金额
    try :
        bank[idx]["balance"] = bank[idx]["balance"] + money
    except:
        return False
    balance2 = bank[idx]["balance"] #存钱后的金额

    if balance1 == balance2:
        if money == 0:

            return True
        else:  #余额增加失败
            return False
    elif balance2 - balance1 ==money:
        # print(balance2) #测试是否增加余额成功，输出增加后的余额
        return True
    else:  #存的钱和账户变化的不一样
        bank[idx]["balance"] = balance1 #余额恢复到未存钱的状态
        return False

# add_balance(11111111,100)
# print(bank[1]["balance"])
# add_balance(11111111,100)

"""
取钱
"""
def reduce_balance(user_name,user_password,money,dict = bank):
    bool = check_user_name(user_name)
    if not bool :
        return 1 #账号不存在
    idx = serchUserIdxByUser_name(user_name)
    password = bank[idx]["user_password"]
    if not user_password == password:
        return 2 #密码不正确
    balance = bank[idx]["balance"]
    if money > balance: #取款金额超过余额
        return 3
    bank[idx]["balance"] = balance - money
    # print(bank[idx]["balance"])# 测试输出

# reduce_balance(11111111,111111,100)
# add_balance(11111111,100)

"""
转账
"""
def transfer_balance(out_user_name,in_user_name,out_user_password,money,dict = bank):
    out_bool = check_user_name(out_user_name) #判断转出账户是否存在
    in_bool = check_user_name(in_user_name) #判断转入账户是否存在
    if not (out_bool and in_bool):
        return 1 #转入或转出账号不存在
    match_bool =  match_user_name_and_user_password(out_user_name,out_user_password)
    if not match_bool: #如果登陆失败
        return 2 #账户密码不匹配
    try :
        out_balance = reduce_balance(out_user_name,out_user_password,money) #转出账户判断余额减少是否成功
        in_balance = add_balance(in_user_name,money)
    except:
        return f"转账失败{out_balance}{in_balance}"
    return "转账成功"

"""
ATM界面
"""
def ATM():
     print("**********************************************")
     print("*          中国农业银行账户管理系统               *")
     print("**********************************************")
     print("*                  选项                       *")
     print("*                 1.开户                      *")
     print("*                 2.存钱                      *")
     print("*                 3.取钱                      *")
     print("*                 4.转账                      *")
     print("*                 5.查询                      *")
     print("*                 6.Bey                      *")
     print("**********************************************")

def main():

    while True:
        ATM()
        operation = int(input("请选择操作:"))

        if operation == 1:
            name = input("请输入姓名:")
            address = input("请输入居住地址：国家+省份+街道+门牌号:")
            open_account(name,address)

        if operation == 2:
            while True:
                user_name = int(input("请输入账户名:"))
                if len(str(user_name)) == 8:
                    break
            money = int(input("请输入存款金额:"))
            add_balance(user_name,money)

        if operation == 3:
            user_name = int(input("请输入账户名:"))
            user_password = int(input("请输入密码:"))
            money = int(input("请输入取款金额"))
            reduce_balance(user_name,user_password, money)

        if operation == 4:
            out_user_name = int(input("请输入转出账户名:"))
            out_user_password = int(input("请输入转出账户名对应的密码:"))
            in_user_name = int(input("请输入收款账户名:"))
            money = int(input("请输入转账金额"))
            transfer_balance(out_user_name, in_user_name, out_user_password, money)

        if operation == 5:
            user_name = int(input("请输入账户名:"))
            user_password = int(input("请输入密码:"))
            serch_user_name(user_name,user_password)

        if operation == 6:
            break
    ATM()
main()


